package com.esender.api.filter.impl;

import com.esender.api.client.BeaconCacheClient;
import com.esender.api.filter.CheckFilter;
import com.esender.common.constant.CacheConstant;
import com.esender.common.enums.ExceptionEnums;
import com.esender.common.exception.ApiException;
import com.esender.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service(value = "apiKey")
@Slf4j
public class ApiKeyCheckFilter implements CheckFilter {
    @Autowired
    private BeaconCacheClient beaconCacheClient;
    @Override
    public void check(StandardSubmit standardSubmit) {
        log.info("【接口模块-校验apikey】   校验ing…………");
        Map clientBusinessMap = beaconCacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS + standardSubmit.getApiKey());
        if (clientBusinessMap == null || clientBusinessMap.isEmpty()) {
            log.info("【接口模块-校验apikey】 非法的apikey = {}",standardSubmit.getApiKey());
            throw new ApiException(ExceptionEnums.ERROR_APIKEY);
        }
        // 校验通过正常封装数据
        standardSubmit.setClientId(Long.parseLong(clientBusinessMap.get("id").toString()));
        log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness = {}",clientBusinessMap);
    }
}
